package com.bjpowerndoe.ylb.web.settings;

//import com.bjpowerndoe.ylb.web.interceptor.TokenInterceptor;
import com.bjpowerndoe.ylb.web.interceptor.TokenInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 *
 */
@Configuration
public class WebMvcSettings  implements WebMvcConfigurer {


    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        TokenInterceptor tokenInterceptor = new TokenInterceptor(stringRedisTemplate);

        String [] addPath = {"/user/realname"};
        registry.addInterceptor(tokenInterceptor)
                .addPathPatterns(addPath);
    }

    //跨域处理
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        System.out.println("===========addCorsMappings===========");
        registry.addMapping("/**")   //拦截所有的请求 , 对这些请求应用跨域处理
                .allowedOriginPatterns("*")     //可跨域的域名，可以为 * 请求的来源
                .allowedMethods("GET", "HEAD", "POST", "PUT", "DELETE", "OPTIONS") //允许跨域的请求方式
                .allowCredentials(true) //cookie
                .maxAge(3600)           //预检的有效时间，秒为单位。
                .allowedHeaders("*");   //可以使用的请求头
    }
}
